package com.snowy.hidden.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 阿里云短信发送
 * @author hjl
 */
@Slf4j
public class AliSmsUtil {
    private static String ACCESS_KEY_ID = "LTAI3Owk6EhTxWZY";
    private static String ACCESS_KEY_SECRET = "hqqv4ZyD3gUbARK8WhBAmiRT9vojNi";
    private static String SIGN_NAME = "星捷科技";
    /**
     * 短信发送
     * @param phoneNumber 手机号
     * @param templateCode 模版编码
     * @param params 短信内容(根据模版传参)
     * @return
     */
    public static SendSmsResponse sendMsg(String phoneNumber,String templateCode,Map<String,String> params){
        Client client = null;
        try {
            client = AliSmsUtil.createClient(ACCESS_KEY_ID,ACCESS_KEY_SECRET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumbers(phoneNumber);
        sendSmsRequest.setSignName(SIGN_NAME);
        sendSmsRequest.setTemplateCode(templateCode);
        sendSmsRequest.setTemplateParam(JSONObject.toJSONString(params));
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse resp = null;
        try {
            resp = client.sendSms(sendSmsRequest);
            log.info(JSONObject.toJSONString(resp));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }
}
